package com.swt;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class DBTool {
//	private final String dirver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
//	private final String url="jdbc:microsoft:sqlserver://192.168.1.3:1061;DatabaseName=qtytemp;SelectMethod=cursor;";
//	private final String username="zhuxiaodong";
//	private final String password="115114";
	private final String dirver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private final String url="jdbc:sqlserver://192.168.30.40:1433;DatabaseName=qtytemp;SelectMethod=cursor;";
	private final String username="sa";
	private final String password="111111";
	private Connection conn;
	private DBTool() {
		try {
			Class.forName(dirver);
			conn = DriverManager.getConnection(url,username,password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public Connection getConnection(){
		return conn;
	}
	public static DBTool newInstance(){
		return new DBTool();
	}
	public void closeConn(Connection conn){
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public void closePstmt(PreparedStatement pstmt){
		if(pstmt!=null){
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public void closeResultSet(ResultSet rs){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
